package com.ysc.task.config;

import com.ysc.task.entity.Task;
import com.ysc.task.mapper.TaskMapper;
import com.ysc.task.task.DynamicTaskManager;
import com.ysc.task.util.EmailTool;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Version: 1.0
 * Class Name: MyInitializer
 * Description: TODO(一句话描述功能)
 * Author: ysc
 * Date: 2024/3/31 19:51
 */
@Component
@RequiredArgsConstructor
public class MyInitializer implements ApplicationRunner {

    private final TaskMapper taskMapper;
    private final DynamicTaskManager dynamicTaskManager;
    private final EmailTool emailTool;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<Task> taskList = taskMapper.selectList(null);
        for (Task task : taskList) {
            if ( task.getIsEnable() == 1 ) {
                dynamicTaskManager.addTask(task.getTaskId().toString(), () -> {
                    dynamicTaskManager.addTask(task.getTaskId().toString(), () -> emailTool.sendEmail(task.getAcceptEmail(),
                            task.getTaskDescription(),
                            task.getSendContent()), task.getTaskCron());
                }, task.getTaskCron());
            }
        }
    }
}
